package com.ys.controller.web.base;

import com.ys.config.MenuId;
import com.ys.entity.base.TbUserTaxManage;
import com.ys.entity.base.TbUserTaxManageItem;
import com.ys.entity.base.TbUserTaxRate;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工个税管理
 */
@Controller
@RequestMapping("/web/base/tbUserTaxManage")
public class WebTbUserTaxManageController {
	private static final Logger logger = LoggerFactory.getLogger(WebTbUserTaxManageController.class);

	@Autowired
	private CommonApiService<TbUserTaxManage> commonApiService;



    /*
     * 编辑
     */
    @GetMapping("/toEdit")
    public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response, Long id) {
        try {
            //TbUserTaxManage entity = new TbUserTaxManage().setId(id);
            commonApiService.toEdit(model,id, new TbUserTaxManage(), MenuId.TB_UserTaxManage, null, null);

            List<Map<String, Object>> taxRateList = new TbUserTaxRate()
                    .setTaxManageId(id)
                    .queryMap();

            List<Map<String, Object>> itemList = new TbUserTaxManageItem()
                    .addField("UA.UserState AS userState")
                   // .addField("U.DeptName AS deptName")
                    .addField("(U.UserId + '/' + U.UserName) AS _ys_nameLinked")
                    .addLeftJoin("TS_UserInfo U ON U.UserId = A.UserId")
                    .addLeftJoin("THR_UserArchives UA ON UA.UserId = A.UserId")
                    .addLeftJoin("TS_OrganizationInfo OI ON OI.Orgid = U.DeptId")
                    .addField("OI.OrgName AS deptName")//部门

                    .setTaxManageId(id)
                    .queryMap();
            DetailList detailList = new DetailList(itemList,taxRateList,id);
            model.addAttribute("detail", detailList);
            model.addAttribute("onProcess", 0);
            return "base/tbUserTaxManage/toEdit";
        }catch (Exception e) {
            logger.error("员工个税管理编辑页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }


    /*
     * 详情
     */
    @GetMapping("/toDetail")
    public String toDetail(Model model, HttpServletRequest request, HttpServletResponse response, Long id) {
        try {
            commonApiService.toEdit(model,id, new TbUserTaxManage(), MenuId.TB_UserTaxManage, null, null);

            List<Map<String, Object>> taxRateList = new TbUserTaxRate()
                    .setTaxManageId(id)
                    .queryMap();

            List<Map<String, Object>> itemList = new TbUserTaxManageItem()
                    .addField("UA.UserState AS userState")
                    .addField("U.DeptName AS deptName")
                    .addField("(U.UserId + '/' + U.UserName) AS _ys_nameLinked")
                    .addLeftJoin("TS_UserInfo U ON U.UserId = A.UserId")
                    .addLeftJoin("THR_UserArchives UA ON UA.UserId = A.UserId")
                    .setTaxManageId(id)
                    .queryMap();
            DetailList detailList = new DetailList(itemList,taxRateList,id);
            model.addAttribute("detail", detailList);
            model.addAttribute("onProcess", 1);
            return "base/tbUserTaxManage/toEdit";
        }catch (Exception e) {
            logger.error("员工个税管理详情页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }



	@Data
	@AllArgsConstructor
	private static class DetailList{
		private List<Map<String, Object>> itemList;
        private List<Map<String, Object>> taxRateList;
		private Long id;
	}


}






